package threadPool;

import java.util.concurrent.*;

/**
 * 默认情况下，和兴线程只有在任务提交的时候才会创建
 * 而预启动策略，可以让核心线程提前启动，从而增强最初提交的线程运行性能
 * prestartCoreThread启动1个核心线程，覆盖仅在执行新任务时启动核心线程的默认策略
 *     有核心线程都已启动，则此方法将返回false
 * prestartCoreThread启动所有和兴线程，覆盖仅在执行新任务时启动核心此案成的默认策略，如果核心线程全部启动后再次调用
 * 则会返回0
 */
public class PreStartAllCoreThreadAllCoreThreadsTest {

    public static void main(String[] args) {
        /**
         * prestartCoreThread启动一个核心此案成
         *  如果所有核心此案成都已启动，则此方法将返回false
         */
//        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
//                2,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>());
//        System.out.println("线程池中线程数 = " + executor.getPoolSize());
//        System.out.println("executor.prestartCoreThread() = " + executor.prestartCoreThread());
//        System.out.println("线程池中线程数 = " + executor.getPoolSize());
//        System.out.println("executor.prestartCoreThread() = " + executor.prestartCoreThread());
//        System.out.println("线程池中线程数 = " + executor.getPoolSize());
//        System.out.println("executor.prestartCoreThread() = " + executor.prestartCoreThread());
//        System.out.println("线程池中线程数 = " + executor.getPoolSize());
//        System.out.println("executor.prestartCoreThread() = " + executor.prestartCoreThread());
        /**
         * 一次启动线程池中所有线程
         */
        prestartCoreThread();
    }


    public static void prestartCoreThread(){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
                2,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>());
        System.out.println("线程池中线程数 = " + executor.getPoolSize());
        System.out.println("executor.prestartCoreThread() = " + executor.prestartAllCoreThreads());
        System.out.println("线程池中线程数 = " + executor.getPoolSize());
        // 返回启动的个数
        int startNum = executor.prestartAllCoreThreads();
        System.out.println("executor.prestartCoreThread() = " + startNum);
        System.out.println("线程池中线程数 = " + executor.getPoolSize());
    }
}
